package com.example.dynamicallyadd;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Gridview_Adapter extends BaseAdapter {
    private Context mContext;
    private boolean mIsOperation;//是否允许对布局进行操作
    private boolean mIsDeleteModel;//删除模式 true：表示可以删除 false：表示不可以删除
    private List<EQ_Fault> mUsers = new ArrayList<>();
    private OnGridviewListener monGridviewListener;

    public Gridview_Adapter(Context context,boolean isOperation,OnGridviewListener onGridviewListener) {
        mContext = context;
        mIsOperation = isOperation;
        monGridviewListener = onGridviewListener;
    }

    public boolean ismIsDeleteModel() {
        return mIsDeleteModel;
    }

    public void setmIsDeleteModel(boolean mIsDeleteModel) {
        this.mIsDeleteModel = mIsDeleteModel;
    }

    //刷新数据
    public void refresh(List<EQ_Fault> users){
        if (users != null && users.size()>=0){
            mUsers.clear();

            //添加加号和减号
            initUsers();

            mUsers.addAll(0,users);
        }
        //刷新页面
        notifyDataSetChanged();
    }
    private void initUsers() {
        EQ_Fault add = new EQ_Fault("add");
        EQ_Fault delete = new EQ_Fault("delete");

        /**
         * 这里添加加减号有讲究。
         * 1.首先添加“减号”（相当于放在0号位置）
         * 2.其次在0号位添加“加号”（相当于把减号位从0号位往右挤）
         * 从而达到我们想要的效果
         */
        mUsers.add(delete);
        mUsers.add(0,add);
    }

    @Override
    public int getCount() {
        return mUsers == null ? 0:mUsers.size();
    }

    @Override
    public Object getItem(int position) {
        return mUsers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        //1 获取或创建viewholder
        ViewHolder viewHolder = null;
        if (view == null){
            viewHolder = new ViewHolder();
            view = View.inflate(mContext,R.layout.item_gridview,null);
            viewHolder.iv_eq_photo = view.findViewById(R.id.iv_eq_photo);
            viewHolder.tv_eq_name = view.findViewById(R.id.tv_eq_name);
            viewHolder.iv_eq_delete = view.findViewById(R.id.iv_eq_delete);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //2 获取当前item数据
        final EQ_Fault user = mUsers.get(position);
        if (mIsOperation){//可操作
            if (position == getCount()-1){//减号的处理
                /**
                 * 如果已经是删除模式，将减号隐藏，其他组件正常显示
                 */
                if (mIsDeleteModel){
                    view.setVisibility(View.INVISIBLE);//隐藏减号
                }else {
                    view.setVisibility(View.VISIBLE);
                    /**
                     * View.VISIBLE--->可见
                     * View.INVISIBLE--->不可见，但这个View仍然会占用在xml文件中所分配的布局空间，不重新layout
                     * View.GONE---->不可见，但这个View在ViewGroup中不保留位置，会重新layout，不再占用空间，那后面的view就会取代他的位置
                     */
                    viewHolder.iv_eq_photo.setImageResource(R.drawable.em_smiley_minus_btn_pressed);
                    viewHolder.iv_eq_delete.setVisibility(View.GONE);
                    viewHolder.tv_eq_name.setVisibility(View.INVISIBLE);
                }
            }else if (position == getCount()-2){//加号的处理
                /**
                 * 如果已经是删除模式，将加号隐藏，其他组件正常显示
                 */
                //删除模式判断
                if (mIsDeleteModel){
                    view.setVisibility(View.INVISIBLE);
                }else {
                    view.setVisibility(View.VISIBLE);
                    viewHolder.iv_eq_photo.setImageResource(R.drawable.em_smiley_add_btn_pressed);
                    viewHolder.iv_eq_delete.setVisibility(View.GONE);
                    viewHolder.tv_eq_name.setVisibility(View.INVISIBLE);
                }
            }else   {
                view.setVisibility(View.VISIBLE);
                viewHolder.tv_eq_name.setVisibility(View.VISIBLE);

                viewHolder.tv_eq_name.setText(user.getEq_fault_id());
                viewHolder.iv_eq_photo.setImageResource(R.drawable.eq);
                if (mIsDeleteModel){
                    viewHolder.iv_eq_delete.setVisibility(View.VISIBLE);
                }else {
                    viewHolder.iv_eq_delete.setVisibility(View.GONE);
                }
            }

            //点击事件的处理
            if (position == getCount() -1){//减号
                viewHolder.iv_eq_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!mIsDeleteModel){//如果不是删除模式
                            //设置为删除模式
                            mIsDeleteModel = true;
                            //刷新页面
                            notifyDataSetChanged();
                        }
                    }
                });
            }else if (position == getCount() -2){//加号
                viewHolder.iv_eq_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        monGridviewListener.onAddMembers();
                    }
                });
            }else {
                viewHolder.iv_eq_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        monGridviewListener.onDeleteMember(user);
                    }
                });
            }
        }else {//不可操作模式
            if (position == getCount() -1 || position == getCount() - 2){
                view.setVisibility(View.GONE);//隐藏加号和减号
            }else {
                view.setVisibility(View.VISIBLE);//其他组件显示

                //名称
                viewHolder.tv_eq_name.setText(user.getEq_fault_id());
                //头像
                viewHolder.iv_eq_photo.setImageResource(R.drawable.eq);
                //删除
                viewHolder.iv_eq_delete.setVisibility(View.GONE);
            }
        }

        return view;
    }
    public interface OnGridviewListener{
        //添加组件方法
        void onAddMembers();

        //删除组件方法
        void onDeleteMember(EQ_Fault user);
    }
    private class ViewHolder{
        private ImageView iv_eq_photo;
        private TextView tv_eq_name;
        private ImageView iv_eq_delete;
    }
}
